package com.yl.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.yl.common.core.domain.AjaxResult;
import com.yl.common.utils.StringUtils;
import com.yl.config.ZhiXingUrlConfig;
import com.yl.domain.TopicDO;
import com.yl.service.TopicService;
import com.yl.service.ZhiXingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {
    private static final Logger log = LoggerFactory.getLogger(TopicServiceImpl.class);
    @Resource
    ZhiXingUrlConfig zhiXingUrlConfig;
    @Resource
    ZhiXingService zhiXingService;


    /**
     * 创建职业
     *
     * @param topicDO
     * @author : y
     * @date :  2024/6/26 9:36
     */
    @Override
    public AjaxResult saveTopic(TopicDO topicDO) {
        List<Long> tagList = topicDO.getSkillTagList();
        if (tagList == null || tagList.size() == 0) {
            return AjaxResult.error("技能标签不能为空");
        }
        if (StringUtils.isBlank(topicDO.getTopicName()) || StringUtils.isBlank(topicDO.getText()) || StringUtils.isBlank(topicDO.getLevel())) {
            return AjaxResult.error("参数为空");
        }
        Long id = null;
        if (topicDO.getId() == null) {
            // 新建职业信息
            // 调用职行创建职业（话题）
            AjaxResult result = zhiXingService.saveDiscussion(topicDO.getTopicName(), topicDO.getCareerMsg(), topicDO.getLabel(), "");
            log.info("调用职行创建职业获取结果={}", result);
            if (result != null && result.get("code").equals(200)) {
                //新增成功
                JSONObject data = JSONObject.parseObject(result.get("data").toString());
                log.info("新增主题成功，对象={}", data);
                id = new Long(data.get("id").toString());
            } else {
                return AjaxResult.error("创建失败，请联系管理员");
            }
        } else {
            // 修改职业信息
            id = topicDO.getId();
            AjaxResult result = zhiXingService.updateDiscussion(topicDO.getTopicName(), topicDO.getCareerMsg(), topicDO.getLabel(), "", id);
            log.info("修改职业信息获取结果={}", result);
            if (result == null || !result.get("code").equals(200)) {
                log.info("修改职业信息失败");
                return AjaxResult.error("修改职业信息失败");
            }
        }
        log.info("调用职行创建题目 text={}\nansAnalysis={}\nlevel={}\ntagList={}\nid={}", topicDO.getText(), topicDO.getAnsAnalysis(), topicDO.getLevel(), tagList, id);
        // 创建题目
        AjaxResult tiKuResult = zhiXingService.createTiKu(topicDO.getText(), topicDO.getAnsAnalysis(), topicDO.getLevel(), tagList, id);
        log.info("职行--创建题目返回参数===result={}", tiKuResult);
        return AjaxResult.success("操作成功");
    }


    /**
     * 获取职业列表
     *
     * @param topicDO
     * @author : y
     * @date :  2024/6/26 9:39
     */
    @Override
    public AjaxResult listTopic(TopicDO topicDO) {
        AjaxResult result = zhiXingService.getDiscussion(1, 10);
        return result;
    }


    /**
     * 删除主题
     *
     * @param topicDO
     * @author : y
     * @date :  2024/6/26 9:37
     */
    @Override
    public AjaxResult deleteTopic(TopicDO topicDO) {
        if (topicDO.getId() == null) {
            return AjaxResult.error("参数为空");
        }
        return zhiXingService.deleteDiscussion(topicDO.getId());
    }

    /**
     * 获取主题详情
     *
     * @param topicId
     * @author : y
     * @date :  2024/6/26 9:37
     */
    @Override
    public AjaxResult getTopicById(Long topicId) {
        if (topicId == null) {
            return AjaxResult.error("参数为空");
        }
        return zhiXingService.getDiscussionById(topicId);
    }
}
